import mx.utils.Delegate;
import mx.events.EventDispatcher;
dynamic class com.live5.controls.LiveButton extends MovieClip
{
	var listener:Object;
	
	// Event dispatch.
	public function addEventListener(type:String,target:Function):Void{};
	public function removeEventListener(type:String,target:Function):Void{};
	public var onDoubleClick:Function;
	private var dispatchEvent:Function;
	
	// Double click.
	private var doubleClickDuration:Number=500;
	private var mouseStatus:Number=0;
	private var intervalId:Number;
	function LiveButton()
	{
		EventDispatcher.initialize(this);
		listener=new Object();
		listener.onMouseMove = Delegate.create(this,OnMouseMove);
		listener.onMouseDown = Delegate.create(this,OnMouseDown);
		listener.onMouseUp = Delegate.create(this,OnMouseUp);
		listener.onMouseWheel = Delegate.create(this,OnMouseWheel);
		Mouse.addListener(listener);
	}

	private function OnMouseMove(){
		
		if(IsMouseOver()){
			//trace("boutton mouse over");
		}
	}
	private function OnMouseDown(){
		
		if(!IsMouseOver()){
			return;
		}
		if(mouseStatus==0){
			trace("start timer");
			StartTimer();
		}
		mouseStatus++;
	}
	

	private function OnMouseUp(){
		if(!IsMouseOver()){
			return;
		}
		mouseStatus++;
		trace(mouseStatus);
		// Mouse down twice and mouse up twice.
		if(mouseStatus>=4){
			StopTimer();
			OnDoubleClick();
		}
		
	}
	private function OnMouseWheel(){
		
	}
	
	private function StartTimer(){
		intervalId=setInterval(this,"StopTimer",doubleClickDuration);
	}
	private function StopTimer(){
       trace("stoptimer");
		mouseStatus=0;
		clearInterval(intervalId);
	}
	[Event("onDoubleClick")]
	private function OnDoubleClick(){
		this.onDoubleClick();
		var eventObj = new Object();
        eventObj.type = "onDoubleClick";
        eventObj.target = this;
		this.dispatchEvent(eventObj);
	}
	private function IsMouseOver():Boolean{
		
		var mousePoint=new Object();
		mousePoint.x = _xmouse;
		mousePoint.y = _ymouse;
		this.localToGlobal(mousePoint);
		var isMouseOver = this.hitTest(mousePoint.x,mousePoint.y,true);
		return isMouseOver;
	}
}
